package com.idanchuang.count.config;

import com.alibaba.cloud.circuitbreaker.sentinel.ReactiveSentinelCircuitBreakerAutoConfiguration;
import com.alibaba.cloud.circuitbreaker.sentinel.SentinelCircuitBreakerAutoConfiguration;
import com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration;
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.cloud.sentinel.endpoint.SentinelEndpointAutoConfiguration;
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration;
import com.idanchuang.component.base.ComponentBaseAutoConfiguration;
import com.idanchuang.component.base.ComponentBaseHealthAutoConfiguration;
import com.idanchuang.component.base.ComponentBasePressureAutoConfiguration;
import com.idanchuang.component.base.ComponentBaseRestTemplateAutoConfiguration;
import com.idanchuang.component.config.apollo.ComponentConfigApolloAutoConfiguration;
import com.idanchuang.component.consumer.ConsumerAutoConfiguration;
import com.idanchuang.component.consumer.ConsumerRibbonConfiguration;
import com.idanchuang.component.consumer.ConsumerServletConfiguration;
import com.idanchuang.component.core.ComponentCoreAutoConfiguration;
import com.idanchuang.component.dispatcher.action.ComponentDispatcherActionAutoConfiguration;
import com.idanchuang.component.just.web.ComponentWebAutoConfiguration;
import com.idanchuang.component.just.web.Swagger2AutoConfiguration;
import com.idanchuang.component.logback.ComponentLogbackAutoConfiguration;
import com.idanchuang.component.logback.ComponentLogbackSentryAutoConfiguration;
import com.idanchuang.component.logback.ComponentLogbackSpringSentryAutoConfiguration;
import com.idanchuang.component.logback.ComponentLogbackWebAutoConfiguration;
import com.idanchuang.component.provider.ProviderAutoConfiguration;
import com.idanchuang.component.redis.ComponentRedisAutoConfiguration;
import com.idanchuang.component.redis.ComponentRedisLimitAutoConfiguration;
import com.idanchuang.component.redis.ComponentRedisLockAutoConfiguration;
import com.idanchuang.component.redis.ComponentRedisPoolAutoConfiguration;
import com.idanchuang.component.sentinel.ComponentSentinelAutoConfiguration;
import com.idanchuang.count.controller.UserCountServiceController;
import com.idanchuang.count.core.aspect.UserCountUpdateAspect;
import com.idanchuang.count.core.aspect.LogAspect;
import com.idanchuang.count.core.model.BizProperties;
import com.idanchuang.count.dao.impl.CountDaoImpl;
import com.idanchuang.count.service.impl.MsgUnreadCountServiceImpl;
import com.idanchuang.count.service.impl.UserCountServiceImpl;
import com.idanchuang.count.support.UserCountChecker;
import org.springframework.boot.actuate.autoconfigure.audit.AuditAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.audit.AuditEventsEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.beans.BeansEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.web.client.HttpClientMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.ribbon.FeignRibbonClientAutoConfiguration;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.testcontainers.containers.GenericContainer;

/**
 * @author Richard_yyf
 * @version 1.0 2021/6/7
 */
@Configuration
@ComponentScan(basePackageClasses = {CountDaoImpl.class, UserCountChecker.class,
        UserCountServiceImpl.class,
        UserCountServiceController.class, MsgUnreadCountServiceImpl.class,
        UserCountUpdateAspect.class, LogAspect.class, BizProperties.class},
        lazyInit = true)
@Lazy
@SpringBootApplication(exclude = {FeignAutoConfiguration.class, ApolloAutoConfiguration.class,
        Swagger2AutoConfiguration.class, ComponentConfigApolloAutoConfiguration.class, ComponentLogbackAutoConfiguration.class,
        ComponentBaseAutoConfiguration.class, ComponentWebAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class,
        ManagementContextAutoConfiguration.class, ComponentBasePressureAutoConfiguration.class,
        ComponentCoreAutoConfiguration.class, ProviderAutoConfiguration.class, JmxAutoConfiguration.class,
        BeansEndpointAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
        TaskExecutionAutoConfiguration.class, AuditAutoConfiguration.class, DataSourceHealthIndicatorAutoConfiguration.class,
        LoadBalancerAutoConfiguration.class, RefreshEndpointAutoConfiguration.class,
        RibbonAutoConfiguration.class, HttpClientMetricsAutoConfiguration.class,
        DataSourcePoolMetricsAutoConfiguration.class, ComponentBaseHealthAutoConfiguration.class,
        ComponentSentinelAutoConfiguration.class, KafkaAutoConfiguration.class,
        ComponentRedisAutoConfiguration.class, ComponentRedisLimitAutoConfiguration.class, ComponentRedisPoolAutoConfiguration.class, ComponentRedisLockAutoConfiguration.class,
        SentinelAutoConfiguration.class, SentinelEndpointAutoConfiguration.class,
        SentinelFeignAutoConfiguration.class, SentinelWebAutoConfiguration.class,
        ReactiveSentinelCircuitBreakerAutoConfiguration.class, SentinelCircuitBreakerAutoConfiguration.class,
        AuditEventsEndpointAutoConfiguration.class,
        ComponentBaseRestTemplateAutoConfiguration.class, ConsumerAutoConfiguration.class,
        ConsumerAutoConfiguration.class, ConsumerServletConfiguration.class, ConsumerRibbonConfiguration.class,
        ComponentDispatcherActionAutoConfiguration.class, ComponentLogbackAutoConfiguration.class,
        ComponentLogbackWebAutoConfiguration.class, ComponentLogbackSentryAutoConfiguration.class, ComponentLogbackSpringSentryAutoConfiguration.class})
public class IntegrationTestConfig {


    private static GenericContainer redis = new GenericContainer<>("redis:5.0.3-alpine")
            .withExposedPorts(6379);

    public static class IntegrationTestInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

        @Override
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {

            redis.start();

            System.out.println("redis.getContainerIpAddress(): " + redis.getContainerIpAddress());
            System.out.println("redis.getMappedPort(): " + redis.getMappedPort(6379));
            TestPropertyValues.of(
                    "spring.redis.host=" + redis.getContainerIpAddress(),
                    "spring.redis.port=" + redis.getMappedPort(6379),
//                    "spring.redis.host=" + "localhost",
//                    "spring.redis.port=" + "6379",
                    "count.msg.expireTimeInSec=7776000",
                    "count.biz.config.bizA.id=1",
                    "count.biz.config.bizA.asyncEnabled=false",
                    "count.biz.config.bizA.asyncExcludeEvents=",
                    "count.biz.config.bizA.uidCheckEnabled=true",
                    "count.biz.config.bizB.id=2",
                    "count.biz.config.bizB.countChangeNotifyEnable=true",
                    "count.biz.config.bizB.countChangeNotifyTopic=count_notify_bizB"
            ).applyTo(configurableApplicationContext.getEnvironment());
        }
    }

}
